package cn.zongjie.day05;

import java.util.ArrayList;
import java.util.Objects;

public class Demo7 {
    public static void main(String[] args) {
        ArrayList<Student> stus = new ArrayList<>();
        String s = "刘若英,50 高圆圆,40 刘诗诗,41 李宇春,35";
        //补全代码  把字符串s里的数据 生成学生对象 添加到stus中
        //按空格分割
        String[] arr = s.split(" ");
        String[] s1 = arr[0].split(",");
        stus.add(new Student(s1[0], Integer.valueOf(s1[1])));
        String[] s2 = arr[1].split(",");
        stus.add(new Student(s2[0], Integer.valueOf(s2[1])));
        String[] s3 = arr[2].split(",");
        stus.add(new Student(s3[0], Integer.valueOf(s2[1])));
        String[] s4 = arr[3].split(",");
        stus.add(new Student(s4[0], Integer.valueOf(s2[1])));
        // System.out.println(stus.get(3).getName());

        stus.add(null);
        stus.add(new Student("刘亦菲", 36));
        stus.add(new Student("刘涛", 38));
        stus.add(new Student("蒋勤勤", 37));
        stus.add(null);
        stus.add(new Student("张子枫", 26));
        stus.add(new Student("杨紫", 30));
        stus.add(new Student("周冬雨", 33));
        // 补全如下:
        //1 利用IFilter 和filterList补全代码 获取集合stus所有非null的数据返回
        ArrayList<Student> stus1 = filterList(stus, Objects::nonNull);
        for (int i = 0; i < stus1.size(); i++) {
            System.out.println(stus1.get(i).toString());
        }
        System.out.println("==============================");
        //2利用IFilter 和filterList补全代码 获取集合stus01里所有大于35岁的演员的数据返回
        ArrayList<Student> stus2 = filterList(stus, stu -> {
            if (Objects.nonNull(stu)) {
                if (stu.getAge() > 35) {
                    return true;
                }
            }
            return false;
        });
        for (int i = 0; i < stus2.size(); i++) {
            System.out.println(stus2.get(i).toString());
        }
        System.out.println("==============================");
        //3利用IFilter 和filterList补全代码 获取集合stus里所有刘姓的演员的数据返回
        ArrayList<Student> stus3 = filterList(stus, stu -> {
            if (Objects.nonNull(stu)) {
                char c = stu.getName().charAt(0);
                if (c == '刘') {
                    return true;
                }
            }
            return false;
        });
        for (int i = 0; i < stus3.size(); i++) {
            System.out.println(stus3.get(i).toString());
        }
    }

    public static ArrayList<Student> filterList(ArrayList<Student> stus, IFilter f) {
        ArrayList<Student> newList = new ArrayList<>();
        //遍历集合
        for (int i = 0; i < stus.size(); i++) {
            //获取每个学生会对象
            Student stu = stus.get(i);
            //判断学生是否符合条件
            if (f.filter(stu)) {
                //符合条件的添加到新的集合里
                newList.add(stu);
            }
        }
        return newList;
    }
}
